<?php
namespace haibao\cms\business\control\pc\action;
use haibao\cms\model\filter\Article as ArticleFilter;
use haibao\cms\model\filter\ArticleTag as ArticleTagFilter;
use haibao\cms\model\filter\ArticleProImage as ArticleProImageFilter;
use haibao\cms\model\filter\ArticleProperty as ArticlePropertyFilter;
use haibao\cms\model\filter\CelebrityImage as CelebrityImageFilter;
use haibao\cms\model\filter\ImageLibrary as ImageLibraryFitler;
use haibao\cms\model\filter\imagecollective\ImageCollective as ImageCollFilter;
use haibao\cms\model\filter\imagecollective\ImageCollectiveTag as ImageCollTagFilter;
use haibao\cms\model\filter\imagecollective\ImageCollectivePro as ImageCollProFilter;
use haibao\cms\model\filter\imagecollective\ImageCollectiveProperty as ImageCollPropertyFilter;
use haibao\cms\model\filter\imagecollective\ImageCollectiveArticle as ImageCollArticleFilter;
use haibao\frame\data\query\Condition;
use haibao\cms\business\control\PublicController as PublicControllerBusiness;
use haibao\cms\data\control\PublicController as PublicControllerData;
use \haibao\frame\data\query\Pager;
use \haibao\cms\business\BusinessException;
use \haibao\cms\business\Tag as TagBusiness;
use \haibao\cms\model\filter\DataMigrationMap as DataMigrationMapFilter;
use \haibao\cms\model\filter\WapInfo as WapInfoFilter;
use \haibao\cms\business\control\pc\entity\ArticleEntity;
use haibao\cms\web\view\app\article\Preview;

class ArticleAction extends BaseAction{

	const QUESY_LIST_FOR_NEW = 1;

	const INFO_TYPE_ARTICLE = 1;//资讯类型 文章
	const INFO_TYPE_IMAGE = 2;//资讯类型 图集
	const INFO_TYPE_FEATURE = 3;//专题
	const INFO_TYPE_VIDEO = 4;//视频
	const INFO_TYPE_ARTICLE_PIC = 3;//资讯类型 文章读图模式
	
	const DATE_TYPE_WEEK = 1;// 1周
	const DATE_TYPE_MONTH = 2;// 1月
	const DATE_TYPE_HALFWEEK = 3;// 半月
	const DATE_TYPE_THREEDAY = 4;//3天之内
	const DATE_TYPE_TWO_DAYS_AGO = 5;//3天前
	const DATE_TYPE_LESSEQUAL_NOWTIME = 6;//小于等于当前时间 
	
	const SORT_DESC = 'desc';
	const SORT_ASC = 'asc';
	
	public $articleEntity;
	private static $_instance;
	
	private $pager;
	private $queryData;
	
	private $articleIdArr = array();
	private $collIdArr = array();
	
	private $tagIdArr = array();
	private $collTagIdArr = array();
	
	private $articles = array();
	private $imageColls = array();
	
	private $articleProImages = array();
	private $collProImages = array();
	
	private $tagInfo = array();
	private $collTagInfo = array();
	
	private $articleTagArr = array();//文章关联标签数组
	private $collTagArr = array();//图集关联标签数组
	
	public static function initArticleAction(){
		if(!self::$_instance instanceof self){
			self::$_instance = new self;
			self::$_instance->queryData = new \haibao\cms\data\Article();
		}
		self::$_instance->initVariable();
		return self::$_instance;
	}
	
	public function setArticleEnity($articleEntity){
		$this->articleEntity = $articleEntity;
	}
	
	/**
	 * 根据标签 发布时间排序 获取文章/图集数据
	 */
	public function getListForNew(){
		
		$this->getArticleForNew();
		if($this->articleEntity->getIsImageColl()){
			$this->getImageCollForNew();
			list($first,$second) = $this->packageReturnData();
			return array($first,$second);
		}else{
			return array($this->articles,$this->articleProImages,$this->tagInfo,$this->articleTagArr,$this->pager);
		}
	}
	private function getArticleForNew(){
		if(!$this->checkEntity()) return null;
		$articleFilter = new ArticleFilter();
		$articleFilter->select(
				array(
						ArticleFilter::CONDITION_FIELD_NAME_ID,
						ArticleFilter::CONDITION_FIELD_NAME_TITLE,
						ArticleFilter::CONDITION_FIELD_NAME_SHORTTITLE,
						ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,
						ArticleFilter::CONDITION_FIELD_NAME_ARTICLE_PUBLISHID,
				        ArticleFilter::CONDITION_FIELD_NAME_SUMMARY
				)
		);
		
		if($this->articleEntity->getOrderByNew()){
		    if($this->articleEntity->orderType == self::SORT_DESC){
		        $articleFilter->order(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,false);
		    }elseif($this->articleEntity->orderType == self::SORT_ASC){
		        $articleFilter->order(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,true);
		    }
		}
		
		if($this->articleEntity->getOrderByClick()){
		    $articlePropertyFilter = new ArticlePropertyFilter();
		    $articlePropertyFilter->select(
		        array(
		            ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
		            ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_EDIT_CLICK_NUM,
		        )
		    );
		    $articleFilter->order(ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_EDIT_CLICK_NUM,false,$articlePropertyFilter);
		    $articleFilter->leftJoin($articlePropertyFilter,null,ArticleFilter::CONDITION_FIELD_NAME_ID,ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_ID,ArticleFilter::CONDITION_FIELD_NAME_ARTICLE_PROPERTY,false);
		}
		
		if($this->articleEntity->getOrderByCommentClick()){
		    $articlePropertyFilter = new ArticlePropertyFilter();
		    $articlePropertyFilter->select(
		        array(
		            ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
		            ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_COMMENT_NUM,
		        )
		    );
		    $articleFilter->order(ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_COMMENT_NUM,false,$articlePropertyFilter);
		    $articleFilter->order(ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_EDIT_CLICK_NUM,false,$articlePropertyFilter);
		    $articleFilter->leftJoin($articlePropertyFilter,null,ArticleFilter::CONDITION_FIELD_NAME_ID,ArticlePropertyFilter::CONDITION_FIELD_NAME_ARTICLE_ID,ArticleFilter::CONDITION_FIELD_NAME_ARTICLE_PROPERTY,false);
		}
		
		
		if($this->articleEntity->getBigLevel() != $this->articleEntity->getLevel()){
			$articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_GREATEREQUAL,$this->articleEntity->getLevel());
			$articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_LESSEQUAL,$this->articleEntity->getBigLevel());
		}else {
			$articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_EQUAL,$this->articleEntity->getLevel());
		}
		
		$articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL, \haibao\cms\model\data\Article::STATUS_ENABLE);
		$articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHED,Condition::CONDITION_EQUAL,\haibao\cms\model\data\Article::PUBLISHED_YES);
	
		if($this->articleEntity->getIsDate()){
			if($this->articleEntity->dateType == self::DATE_TYPE_WEEK){
				$articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-1 week')),date('Y-m-d')));
			}elseif($this->articleEntity->dateType == self::DATE_TYPE_MONTH){
			    $articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-1 month')),date('Y-m-d')));
			}elseif($this->articleEntity->dateType == self::DATE_TYPE_HALFWEEK){
			    $articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-14 days')),date('Y-m-d')));
			}elseif($this->articleEntity->dateType == self::DATE_TYPE_THREEDAY){
			    $articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-3 days')),date('Y-m-d')));
			}elseif($this->articleEntity->dateType == self::DATE_TYPE_TWO_DAYS_AGO){
				$articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,Condition::CONDITION_LESSEQUAL,date('Y-m-d',strtotime('-3 days')));
			}elseif($this->articleEntity->dateType == self::DATE_TYPE_LESSEQUAL_NOWTIME){
			    $articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,Condition::CONDITION_LESSEQUAL,date('Y-m-d H:i:s'));
			}
		}
		
		$notArticleId = array();
		if($this->articleEntity->getNotInId()){
		    foreach($this->articleEntity->infoIds as $key=>$val){
		        if($val['IdType'] == self::INFO_TYPE_ARTICLE){
		            $notArticleId[] = $val['Id'];
		        }
		    }
		    if($notArticleId){
		        $articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_NOTIN,$notArticleId);
		    }
		}
		
		if($this->articleEntity->getIsLeftJoinTag()){
			$articleTagFilter = new ArticleTagFilter();
			$articleTagFilter->select(
					array(
							ArticleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
							ArticleTagFilter::CONDITION_FIELD_NAME_TAG_ID,
							ArticleTagFilter::CONDITION_FIELD_NAME_PARENT_ID,
							ArticleTagFilter::CONDITION_FIELD_NAME_IS_PRIMAIY,
					)
			);
			if($this->articleEntity->getIsConditionTag()){
				if(is_array($this->articleEntity->getTagIds())){
					if($this->articleEntity->getIsParentTag()){
						$articleTagFilter->where(ArticleTagFilter::CONDITION_FIELD_NAME_PARENT_ID,Condition::CONDITION_IN,$this->articleEntity->getTagIds());
					}else{
						$articleTagFilter->where(ArticleTagFilter::CONDITION_FIELD_NAME_TAG_ID,Condition::CONDITION_IN,$this->articleEntity->getTagIds());
					}
				}else{
					if($this->articleEntity->getIsParentTag()){
						$articleTagFilter->where(ArticleTagFilter::CONDITION_FIELD_NAME_PARENT_ID,Condition::CONDITION_EQUAL,$this->articleEntity->getTagIds());
					}else{
						$articleTagFilter->where(ArticleTagFilter::CONDITION_FIELD_NAME_TAG_ID,Condition::CONDITION_EQUAL,$this->articleEntity->getTagIds());
					}
				}
				if($this->articleEntity->getIsPrimaryTag()){
					$articleTagFilter->where(ArticleTagFilter::CONDITION_FIELD_NAME_IS_PRIMAIY,Condition::CONDITION_EQUAL,\haibao\cms\model\data\ArticleTag::PRIMAIY_IS);
				}
			}else{
				if($this->articleEntity->getIsPrimaryTag()){
					$articleTagFilter->where(ArticleTagFilter::CONDITION_FIELD_NAME_IS_PRIMAIY,Condition::CONDITION_EQUAL,\haibao\cms\model\data\ArticleTag::PRIMAIY_IS);
				}
			}
			$articleFilter->leftJoin($articleTagFilter,null,ArticleFilter::CONDITION_FIELD_NAME_ID,ArticleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,ArticleFilter::CONDITION_FIELD_NAME_ARTICLE_TAG,false);
		}
		if($this->articleEntity->getIsPage()){
			$articleFilter->setPager(new Pager($this->articleEntity->getLimit(), $this->articleEntity->getCurrentPage()));
			$this->pager = $articleFilter->getPager();
		}else{
		    $articleFilter->limit($this->articleEntity->getOffset(),$this->articleEntity->getLimit());
		}
		if($this->articleEntity->getIsDebug()){
			
			print_r($this->articleEntity->getTagIds());exit;
		}
		$articles = $this->queryData->getAll($articleFilter);
		if($this->articleEntity->getIsDebug()){
		}
		if($this->articleEntity->getIsPage()){
			if($this->articleEntity->getCurrentPage() !=$this->pager->getCurrentPage()) {
				throw new BusinessException('数据为空',self::DATA_EMPTY);
			}
		}
	
		$count = count($articles);
		foreach($articles as $key=>$article){
			$article->SiteType = self::INFO_TYPE_ARTICLE;
			if($this->articleEntity->getOrderByNew()){
			    $publishTime = strtotime($article->PublishTime->format('Y-m-d H:i:s'))+$count-$key;
			    $this->articles[$publishTime] = $article;
			}elseif($this->articleEntity->getOrderByClick()){
			    $editClickNum = $article->ArticleProperty->EditClickNum + $count-$key;
			    $this->articles[$editClickNum] = $article;
			}elseif($this->articleEntity->getOrderByCommentClick()){
			    $commentClickNum = $article->ArticleProperty->CommentNum + $count-$key;
			    $this->articles[$commentClickNum] = $article;
			}
			array_push($this->articleIdArr,$article->Id);
		}
		if($this->articleEntity->getIsProImage()){
			if($this->articleIdArr){
				$this->articleProImages = $this->getArticleProImage($this->articleIdArr,$this->articleEntity->getProImageLevel());
			}
		}
		if($this->articleEntity->getIsReturnTag()){
		    $this->articleTagArr = $this->getArticleTagByArticleId($this->articleIdArr,$this->articleEntity->getIsOnlyReturnPrimaryTag());
			if($this->articleEntity->getIsPrimaryTag()){
				foreach($this->articles as $article){
					array_push($this->tagIdArr,$article->ArticleTag->TagId);
				}
				if($this->tagIdArr){
					$this->tagInfo = PublicControllerBusiness::getTagInfoById($this->tagIdArr,$this->articleEntity->getIsDebug());
				}
			}else{
				$tmpTagIds = array();
				if($this->articleEntity->getIsOnlyReturnPrimaryTag()){
					foreach($this->articleTagArr as $arr){
						array_push($tmpTagIds,$arr['TagId']);
					}
				}else{
					foreach($this->articleTagArr as $key=>$arr){
						$tmpTagIds = array_merge($tmpTagIds,array_reduce($arr, create_function('$v,$w', '$v[$w["TagId"]]=$w["TagId"];return $v;')));
					}
				}
				if($tmpTagIds){
					$this->tagInfo = PublicControllerBusiness::getTagInfoById(array_unique($tmpTagIds),$this->articleEntity->getIsDebug());
				}
			}
		}
	}
	public function getImageCollForNew($isRemoveArticle = true){
		$imageCollFilter = new ImageCollFilter();
		$imageCollFilter->select(
				array(
						ImageCollFilter::CONDITION_FIELD_NAME_ID,
						ImageCollFilter::CONDITION_FIELD_NAME_TITLE,
						ImageCollFilter::CONDITION_FIELD_NAME_SHORT_TITLE,
						ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,
				)
		);
		
		if($this->articleEntity->getOrderByNew()){
		    if($this->articleEntity->orderType == self::SORT_DESC){
		       $imageCollFilter->order(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,false);
		    }elseif($this->articleEntity->orderType == self::SORT_ASC){
		       $imageCollFilter->order(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,true);
		    }
		}
		
		if($this->articleEntity->getOrderByClick()){
		    $imageCollPropertyFilter = new ImageCollPropertyFilter;
		    $imageCollPropertyFilter->select(
		        array(
		            ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,
		            ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_EDITCLICK_NUM,
		        )
		    );
		    $imageCollFilter->order(ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_EDITCLICK_NUM,false,$imageCollPropertyFilter);
		    $imageCollFilter->leftJoin($imageCollPropertyFilter,null,ImageCollFilter::CONDITION_FIELD_NAME_ID,ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PROPERTY,false);
		}
		
		if($this->articleEntity->getOrderByCommentClick()){
		     $imageCollPropertyFilter = new ImageCollPropertyFilter;
		    $imageCollPropertyFilter->select(
		        array(
		            ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,
		            ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_COMMENT_NUM,
		        )
		    );
		    $imageCollFilter->order(ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_COMMENT_NUM,false,$imageCollPropertyFilter);
		    $imageCollFilter->order(ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_EDITCLICK_NUM,false,$imageCollPropertyFilter);
		    $imageCollFilter->leftJoin($imageCollPropertyFilter,null,ArticleFilter::CONDITION_FIELD_NAME_ID,ImageCollPropertyFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PROPERTY,false);
		}
		
		
		$bigLevel = $this->articleEntity->getBigLevel();
		$level = $this->articleEntity->getLevel();
		if($bigLevel != $level){
			$imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_GREATEREQUAL,$level);
			$imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_LESSEQUAL,$bigLevel);
		}else {
			$imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_EQUAL,$level);
		}
	
		$imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL, \haibao\cms\model\data\imagecollective\ImageCollective::STATUS_ENABLE);
		$imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHED,Condition::CONDITION_EQUAL,\haibao\cms\model\data\imagecollective\ImageCollective::STATUS_YES_PUBLISH);
	
	
		if($isRemoveArticle){
			if($this->articleIdArr){
				$imageCollArticleFilter = new ImageCollArticleFilter();
				if(is_array($this->articleIdArr)){
					$imageCollArticleFilter->where(ImageCollArticleFilter::CONDITION_FIELD_NAME_ARITCLE_ID, Condition::CONDITION_OR,array(
							new Condition(ImageCollArticleFilter::CONDITION_FIELD_NAME_ARITCLE_ID, Condition::CONDITION_NOTIN, $this->articleIdArr),
							new Condition(ImageCollArticleFilter::CONDITION_FIELD_NAME_ARITCLE_ID, Condition::CONDITION_IS_NULL, null),
					));
				}else{
					$imageCollArticleFilter->where(ImageCollArticleFilter::CONDITION_FIELD_NAME_ARITCLE_ID, Condition::CONDITION_OR,array(
							new Condition(ImageCollArticleFilter::CONDITION_FIELD_NAME_ARITCLE_ID, Condition::CONDITION_NOTEQUAL, $this->articleIdArr),
							new Condition(ImageCollArticleFilter::CONDITION_FIELD_NAME_ARITCLE_ID, Condition::CONDITION_IS_NULL, null),
					));
				}
				$imageCollFilter->leftjoin($imageCollArticleFilter,null,ImageCollFilter::CONDITION_FIELD_NAME_ID,ImageCollArticleFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID);
			}
		}
		
		if($this->articleEntity->getIsDate()){
		    if($this->articleEntity->dateType == self::DATE_TYPE_WEEK){
		        $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-1 week')),date('Y-m-d')));
		    }elseif($this->articleEntity->dateType == self::DATE_TYPE_MONTH){
		        $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-1 month')),date('Y-m-d')));
		    }elseif($this->articleEntity->dateType == self::DATE_TYPE_HALFWEEK){
			    $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-14 days')),date('Y-m-d')));
			}elseif($this->articleEntity->dateType == self::DATE_TYPE_THREEDAY){
		        $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,Condition::CONDITION_BETWEEN,array(date('Y-m-d',strtotime('-3 days')),date('Y-m-d')));
		    }elseif($this->articleEntity->dateType == self::DATE_TYPE_TWO_DAYS_AGO){
				$imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,Condition::CONDITION_LESSEQUAL,date('Y-m-d',strtotime('-3 days')));
			}elseif($this->articleEntity->dateType == self::DATE_TYPE_LESSEQUAL_NOWTIME){
			    $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_PUBLISHTIME,Condition::CONDITION_LESSEQUAL,date('Y-m-d H:i:s'));
			}
		}
	
		$notImageCollectiveId = array();
		if($this->articleEntity->getNotInId()){
		    foreach($this->articleEntity->infoIds as $key=>$val){
		        if($val['IdType'] == self::INFO_TYPE_IMAGE){
		            $notImageCollectiveId[] = $val['Id'];
		        }
		    }
		    if($notImageCollectiveId){
		        $imageCollFilter->where(ImageCollFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_NOTIN,$notImageCollectiveId);
		    }
		}
		
		if($this->articleEntity->getIsLeftJoinTag()){
			$imageCollTagFilter = new ImageCollTagFilter();
			$imageCollTagFilter->select(
					array(
							ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,
							ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ID,
							ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_PARENTID,
							ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ISPRIMAIY,
					)
			);
			if($this->articleEntity->getIsConditionTag()){
				if(is_array($this->articleEntity->getTagIds())){
					if($this->articleEntity->getIsParentTag()){
						$imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_PARENTID,Condition::CONDITION_IN,$this->articleEntity->getTagIds());
					}else{
						$imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ID,Condition::CONDITION_IN,$this->articleEntity->getTagIds());
					}
				}else{
					if($this->articleEntity->getIsParentTag()){
						$imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_PARENTID,Condition::CONDITION_EQUAL,$this->articleEntity->getTagIds());
					}else{
						$imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ID,Condition::CONDITION_EQUAL,$this->articleEntity->getTagIds());
					}
				}
				if($this->articleEntity->getIsPrimaryTag()){
					$imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ISPRIMAIY,Condition::CONDITION_EQUAL,\haibao\cms\model\data\imagecollective\ImageCollectiveTag::IS_PRIMAIY);
				}
			}else{
				if($this->articleEntity->getIsPrimaryTag()){
					$imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ISPRIMAIY,Condition::CONDITION_EQUAL,\haibao\cms\model\data\imagecollective\ImageCollectiveTag::IS_PRIMAIY);
				}
			}
			$imageCollFilter->leftJoin($imageCollTagFilter,null,ImageCollFilter::CONDITION_FIELD_NAME_ID,ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,ImageCollFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_TAG,false);
		}
		
		if($this->articleEntity->getIsPage()){
		    $imageCollFilter->setPager(new Pager($this->articleEntity->getLimit(), $this->articleEntity->getCurrentPage()));
		    $this->pager = $imageCollFilter->getPager();
		}else{
		    $imageCollFilter->limit($this->articleEntity->getOffset(),$this->articleEntity->getLimit());
		}
		
		$imageColls = $this->queryData->getAll($imageCollFilter);
		$count = count($imageColls);
		foreach($imageColls as $key=>$imageColl){
			$imageColl->SiteType = self::INFO_TYPE_IMAGE;
			if($this->articleEntity->getOrderByNew()){
			    $time = strtotime($imageColl->PublishTime->format('Y-m-d H:i:s'))+$count-$key;
			    $this->imageColls[$time] = $imageColl;
			}elseif($this->articleEntity->getOrderByClick()){
			    $editClickNum = $imageColl->ImageCollectiveProperty->EditClickNum + $count-$key;
			    $this->imageColls[$editClickNum] = $imageColl;
			}elseif($this->articleEntity->getOrderByCommentClick()){
			    $commentClickNum = $imageColl->ImageCollectiveProperty->CommentNum + $count-$key;
			    $this->imageColls[$commentClickNum] = $imageColl ;
			}
			array_push($this->collIdArr,$imageColl->Id);
				
		}

		if($this->articleEntity->getIsProImage()){
			if($this->collIdArr){
				$this->collProImages = $this->getCollProImage($this->collIdArr,$this->articleEntity->getProImageLevel());
			}
		}
		if($this->articleEntity->getIsReturnTag()){
		    $this->collTagArr = $this->getCollTagByCollId($this->collIdArr,$this->articleEntity->getIsOnlyReturnPrimaryTag());
			if($this->articleEntity->getIsPrimaryTag()){
				foreach($this->imageColls as $coll){
					array_push($this->collTagIdArr,$coll->ImageCollectiveTag->TagId);
				}
				$this->collTagIdArr = array_unique($this->collTagIdArr);
				if($this->collTagIdArr){
					$this->collTagInfo = PublicControllerBusiness::getTagInfoById($this->collTagIdArr);
				}
			}else{
				if($this->collTagArr){
					$tmpTagIds = array();
					if($this->articleEntity->getIsOnlyReturnPrimaryTag()){
						foreach($this->collTagArr as $arr){
							array_push($tmpTagIds,$arr['TagId']);
						}
					}else{
						foreach($this->collTagArr as $key=>$arr){
							$tmpTagIds = array_merge($tmpTagIds,array_reduce($arr, create_function('$v,$w', '$v[$w["TagId"]]=$w["TagId"];return $v;')));
						}
					}
					$this->collTagInfo = PublicControllerBusiness::getTagInfoById(array_unique($tmpTagIds),$this->articleEntity->getIsDebug());
				}
			}
		}
	}
	/**
	 * 获取文章导读图
	 * @param : Array $articleIdArr 文章ID数组
	 * @param : Array $level 导读图等级
	 */
	public function getArticleProImage($articleIdArr,$proImageLevel = ''){
		
		if(!is_array($proImageLevel)) $proImageLevel = (array)$proImageLevel;
		array_push($proImageLevel,\haibao\cms\model\data\ArticleProImage::Level_ZERO);
		$articleProImageFilter = new ArticleProImageFilter();
		$articleProImageFilter->select(
				array(
						ArticleProImageFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
						ArticleProImageFilter::CONDITION_FIELD_NAME_LEVEL,
						ArticleProImageFilter::CONDITION_FIELD_NAME_PIC_URL,
				)
		);
		$articleProImageFilter->where(ArticleProImageFilter::CONDITION_FIELD_NAME_PIC_TYPE,Condition::CONDITION_EQUAL,\haibao\cms\model\data\ArticleProImage::PIC_TYPE_INTRO);
		
		$articleProImageFilter->where(ArticleProImageFilter::CONDITION_FIELD_NAME_LEVEL,Condition::CONDITION_IN,$proImageLevel);
		$articleProImageFilter->where(ArticleProImageFilter::CONDITION_FIELD_NAME_ARTICLE_ID,Condition::CONDITION_IN,$articleIdArr);
	
		$articleProImages = \haibao\cms\business\Article::getArticleList($articleProImageFilter);
		$proImageArr = array();
		$articleIds = array();
		foreach($articleProImages as $proImage){
			if(isset($proImageArr[$proImage->ArticleId])){
				$proImageArr[$proImage->ArticleId][$proImage->Level] = $proImage->PicUrl;
			}else{
				$proImageArr[$proImage->ArticleId] = array();
				$proImageArr[$proImage->ArticleId][$proImage->Level] = $proImage->PicUrl;
			}
			$articleIds[] = $proImage->ArticleId;
		}
	
		$articleDiffIds = array_diff($this->articleIdArr, $articleIds);
		if($articleDiffIds){
			foreach ($articleDiffIds as $articleId){
				$proImageArr[$articleId] = array();
				$proImageArr[$articleId][\haibao\cms\model\data\ArticleProImage::Level_ZERO] = \haibao\cms\business\control\PublicController::getArticleFirstImage($articleId);
			}
		}
		return $proImageArr;
	}
	/**
	 * 获取图集导读图
	 * @param : Array $articleIdArr 文章ID数组
	 * @param : Array $level 导读图等级
	 *
	 */
	public function getCollProImage($collIdArr,$proImageLevel = ''){
		if(!is_array($proImageLevel)) $proImageLevel = (array)$proImageLevel;
		array_push($proImageLevel,\haibao\cms\model\data\imagecollective\ImageCollectivePro::IMAGE_TYPE_ZERO);
		if(!is_array($collIdArr)) $collIdArr = (array)$collIdArr;
		$imageCollProFilter = new ImageCollProFilter();
		$imageCollProFilter->select(
				array(
						ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,
						ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_IMAGE_TYPE,
						ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_ID,
				)
		);
		$imageLibraryFilter = new ImageLibraryFitler();
		$imageLibraryFilter->select(array(
				ImageLibraryFitler::CONDITION_FIELD_NAME_FILENAME,
		));
	
		$imageCollProFilter->where(ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_IMAGE_TYPE,Condition::CONDITION_IN,$proImageLevel);
		$imageCollProFilter->where(ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_COLLECTIVE_ID,Condition::CONDITION_IN,$collIdArr);
		$imageCollProFilter->leftJoin($imageLibraryFilter,null,ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_ID,ImageLibraryFitler::CONDITION_FIELD_NAME_ID,ImageCollProFilter::CONDITION_FIELD_NAME_IMAGE_FILENAME,false);
	
		$collProImages = $this->queryData->getAll($imageCollProFilter);
	
		$proImageArr = array();
		foreach($collProImages as $proImage){
			if(isset($proImageArr[$proImage->ImageCollectiveId])){
				$proImageArr[$proImage->ImageCollectiveId][$proImage->ImageType] = $proImage->FileName->Filename;
			}else{
				$proImageArr[$proImage->ImageCollectiveId] = array();
				$proImageArr[$proImage->ImageCollectiveId][$proImage->ImageType] = $proImage->FileName->Filename;
			}
		}
		return $proImageArr;
	}

	/**
	 * @param Array $articleId 外部调用传递的文章ID
	 * @param Boolure $isReturnPrimaryTag
	 */
	public function getArticleTagByArticleId($articleId,$isReturnPrimaryTag = true){
	
		$articleTagFilter = new ArticleTagFilter();
		$articleTagFilter->select(
				array(
						ArticleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
						ArticleTagFilter::CONDITION_FIELD_NAME_TAG_ID,
						ArticleTagFilter::CONDITION_FIELD_NAME_PARENT_ID,
						ArticleTagFilter::CONDITION_FIELD_NAME_IS_PRIMAIY,
				)
		);
		$articleTagArr = array();
		if($articleId){
			$articleId = (array)$articleId;
			$articleTagFilter->where(ArticleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,Condition::CONDITION_IN,$articleId);
			$articleTags = $this->queryData->getAll($articleTagFilter);
			
			if($articleTags){
			    foreach($articleTags as $articleTag){
			
			        if(!isset($articleTagArr[$articleTag->ArticleId])){
			            $articleTagArr[$articleTag->ArticleId] = array();
			        }
			        if($isReturnPrimaryTag){
			            if($articleTag->IsPrimaiy == \haibao\cms\model\data\ArticleTag::PRIMAIY_IS){
			                $articleTagArr[$articleTag->ArticleId]['TagId'] = $articleTag->TagId;
			                $articleTagArr[$articleTag->ArticleId]['ParentId'] = $articleTag->ParentId;
			            }
			
			        }else{
			
			            $temp = array();
			            $temp['TagId'] = $articleTag->TagId;
			            $temp['ParentId'] = $articleTag->ParentId;
			            $temp['IsPrimaiy'] = $articleTag->IsPrimaiy;
			            if(!isset($articleTagArr[$articleTag->ArticleId])) $articleTagArr[$articleTag->ArticleId] = array();
			            $articleTagArr[$articleTag->ArticleId][] = $temp;
			        }
			    }
			}
			
			foreach($articleTagArr as $key=>$val){
			    if(empty($val)) {
			        $articleTagArr[$key]['TagId'] = self::HAI_BAO_WANG_TAG;
			        $articleTagArr[$key]['ParentId'] = self::HAI_BAO_WANG_TAG;
			    }
			
			}
		}
		return $articleTagArr;
	}
	/**
	 * @param Array $collId 外部调用传递的文章ID
	 * @param Boolure $isReturnPrimaryTag
	 */
	public function getCollTagByCollId($collId = '',$isReturnPrimaryTag = true){
		$collTagArr = array();
	
		$imageCollTagFilter = new ImageCollTagFilter();
		$imageCollTagFilter->select(
				array(
						ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,
						ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ID,
						ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_PARENTID,
						ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_ISPRIMAIY,
				)
		);
		if($collId){
			$collId = (array)$collId;
			$imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,Condition::CONDITION_IN,$collId);
		}else{
			if(!$this->collIdArr){
				return $collTagArr;
			}
			$imageCollTagFilter->where(ImageCollTagFilter::CONDITION_FIELD_NAME_TAG_COLLECTIVE_ID,Condition::CONDITION_IN,$this->collIdArr);
		}
	
		$collTags = $this->queryData->getAll($imageCollTagFilter);
		$collTagArr = array();
		if($collTags){
			foreach($collTags as $collTag){
	
				if($isReturnPrimaryTag){
					if($collTag->IsPrimaiy == \haibao\cms\model\data\imagecollective\ImageCollectiveTag::IS_PRIMAIY){
						$collTagArr[$collTag->ImageCollectiveId]['TagId'] = $collTag->TagId;
						$collTagArr[$collTag->ImageCollectiveId]['ParentId'] = $collTag->ParentId;
					}
				}else{
	
					if(!isset($collTagArr[$collTag->ImageCollectiveId])){
						$collTagArr[$collTag->ImageCollectiveId] = array();
					}
	
					$temp = array();
					$temp['TagId'] = $collTag->TagId;
					$temp['ParentId'] = $collTag->ParentId;
					$temp['IsPrimaiy'] = $collTag->IsPrimaiy;
					$collTagArr[$collTag->ImageCollectiveId][] = $temp;
						
				}
			}
		}
		foreach($collTagArr as $key=>$val){
			if(empty($val)) $collTagArr[$key] = self::HAI_BAO_WANG_TAG;
		}
		return $collTagArr;
	}
	private function packageReturnData(){
	
		$article = array(
				'list'=>$this->articles,
				'proImage'=>$this->articleProImages,
				'tag'=>$this->tagInfo,
				'articleTag'=>$this->articleTagArr
	
		);
		$imageColl = array(
				'list'=>$this->imageColls,
				'proImage'=>$this->collProImages,
				'tag'=>$this->collTagInfo,
				'collTag'=>$this->collTagArr
		);
		return array($article,$imageColl);
	}
	/**
	 * 检验实体
	 */
	public function checkEntity(){
		if($this->articleEntity instanceof ArticleEntity){
			return true;
		}
		return false;
	}
	
	private function initVariable(){
		$this->articleIdArr = array();
		$this->collIdArr = array();
		
		$this->tagIdArr = array();
		$this->collTagIdArr = array();
		
		$this->articles = array();
		$this->imageColls = array();
		
		$this->articleProImages = array();
		$this->collProImages = array();
		
		$this->tagInfo = array();
		$this->collTagInfo = array();
		
		$this->articleTagArr = array();//文章关联标签数组
		$this->collTagArr = array();//图集关联标签数组
	}
}
